package cn.gok.web;

import cn.gok.entity.Student;
import cn.gok.entity.TStudent;
import cn.gok.entity.Teacher;
import cn.gok.service.NewStudentService;
import cn.gok.service.impl.NewStudentServiceImpl;
import cn.gok.util.R;
import cn.hutool.json.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/newStudent")
public class NewStudentServlet extends HttpServlet {
    private NewStudentService newStudentService=new NewStudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        switch (action){
            case "getStudentInfo" :getStudentInfo(req,resp);break;//查询学生信息
            case "checkPassword":checkPassword(req, resp);break;//校验密码
            case "updatePassword":updatePassword(req, resp);break;//修改密码
            case "selectCourse":selectCourse(req, resp);break;//查询课程
            case "selectCourseById":selectCourseById(req, resp);break;//获取课程信息
            case "chooseCourse":chooseCourse(req, resp);break;//选课
            case "selectMyCourse":selectMyCourse(req, resp);break;//查询自己的课程
            case "deleteCourse":deleteCourse(req, resp);break;//取消选课
        }
    }
    /*
     *  获取用户的基础信息
     * */
    public void getStudentInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student =(Student) req.getSession().getAttribute("student");
        R data = R.create(student);
        resp.getWriter().println(JSONUtil.toJsonStr(data));
    }

    //查询之前的密码
    public void checkPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student = (Student) req.getSession().getAttribute("student");
        String password = req.getParameter("password");
        R r = newStudentService.checkPassword(password, student.getStuId());
        resp.getWriter().println(JSONUtil.toJsonStr(r));

    }
    /*
     * 修改密码
     * */
    public void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student =(Student) req.getSession().getAttribute("student");
        Integer stuId = student.getStuId();
        String password = req.getParameter("password");
        R r = newStudentService.changePassword(password, stuId);
        String str = JSONUtil.toJsonStr(r);
        resp.getWriter().println(str);
    }

    //课程查询
    public void selectCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String couName = req.getParameter("couName");
        String place = req.getParameter("place");
        String state = req.getParameter("state");

        int currPage = Integer.parseInt(req.getParameter("currPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        R r = newStudentService.selectCourse(couName, place, state, currPage, pageSize);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }

    //获取课程详情
    public void selectCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String couId = req.getParameter("couId");
        R r = newStudentService.selectCourseById(couId);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }

    //选课
    public void chooseCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
       Student student = (Student) req.getSession().getAttribute("student");
        String  stuId = String.valueOf(student.getStuId());
        String couId = req.getParameter("couId");
        String state = req.getParameter("state");
        String max = req.getParameter("max");
        String num = req.getParameter("num");
        R r = newStudentService.chooseCourse(couId, stuId, state,max,num);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }

    //查询我的课程
    public void selectMyCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student = (Student) req.getSession().getAttribute("student");
        String  stuId = String.valueOf(student.getStuId());
        int currPage = Integer.parseInt(req.getParameter("currPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        R r = newStudentService.selectMyCourse(stuId, currPage, pageSize);
        resp.getWriter().println(JSONUtil.toJsonStr(r));

    }

    //取消选课
    public void deleteCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Student student = (Student) req.getSession().getAttribute("student");
        String  stuId = String.valueOf(student.getStuId());
        String couId = req.getParameter("couId");
        R r = newStudentService.deleteCourse(couId, stuId);
        resp.getWriter().println(JSONUtil.toJsonStr(r));
    }

}
